package org.ljy.forum6.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.ljy.forum6.common.AppConfig;
import org.ljy.forum6.common.AppResult;
import org.ljy.forum6.common.ResultCode;
import org.ljy.forum6.exception.ApplicationException;
import org.ljy.forum6.model.Article;
import org.ljy.forum6.model.ArticleReply;
import org.ljy.forum6.model.User;
import org.ljy.forum6.services.impl.ArticleReplyServiceImpl;
import org.ljy.forum6.services.impl.ArticleServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Api(tags = "帖子回复接口")
@Slf4j
@RequestMapping("/reply")
@RestController
public class ArticleReplyController {

    @Resource
    private ArticleReplyServiceImpl articleReplyService;

    @Resource
    private ArticleServiceImpl articleService;

    @ApiOperation("发布回复内容")
    @PostMapping("/create")
    public AppResult<ArticleReply> create(HttpServletRequest request,
                                          @ApiParam("帖子id") @RequestParam("articleId") @NonNull Long articleId,
                                          @ApiParam("回复内容") @RequestParam("content") @NonNull String content) {
        //1.校验作者信息
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState()==1) {
            log.warn(ResultCode.FAILED_USER_BANNED.toString());
            return AppResult.failed(ResultCode.FAILED_USER_BANNED.toString());
        }
        //2.校验文章
        Article article = articleService.selectById(articleId);
        if(article.getDeleteState()==1 || article.getState() == 1) {
            log.warn(ResultCode.FAILED_USER_BANNED.toString());
            return AppResult.failed(ResultCode.FAILED_USER_BANNED.toString());
        }
        //3.构建回复对象
        ArticleReply articleReply = new ArticleReply();
        articleReply.setArticleId(articleId);
        articleReply.setContent(content);
        articleReply.setPostUserId(user.getId());
        //3.调用service
        articleReplyService.create(articleReply);

        return AppResult.success();
    }

    @ApiOperation("回复列表")
    @GetMapping("/getReplies")
    public AppResult<List<ArticleReply>> getRepliesByArticleId(@ApiParam("文章id")
                                                               @RequestParam("articleId")
                                                               @NonNull  Long articleId) {
        //1.参数、文章合法性验证
        if(articleId<=0) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        Article article = articleService.selectById(articleId);
        if(article == null || article.getDeleteState()==1 || article.getState() == 1) {
            log.warn(ResultCode.FAILED_PARAMS_VALIDATE.toString());
            throw new ApplicationException(AppResult.failed(ResultCode.FAILED_PARAMS_VALIDATE));
        }
        //2.调用service
        List<ArticleReply> articleReplies = articleReplyService.selectByArticleId(articleId);

        return AppResult.success(articleReplies);
    }



}
